package com.wms.web.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.wms.model.PageBean;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;

/**
 * 公共基础类
 *
 * @param <T> 模型
 */
public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T> {
    /**
     * 模型
     */
    private T t;

    protected String ids;

    @SuppressWarnings("unchecked")
    public BaseAction() {
        ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
        Type[] types = pt.getActualTypeArguments();
        Class<T> clz = (Class<T>) types[0];
        //设置离线查询对象
        /*DetachedCriteria dc = DetachedCriteria.forClass(clz);
        pb.setDetachedCriteria(dc);*/
        try {
            t = clz.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void responseJson(Object obj) throws IOException {
        responseJson(obj, null);
    }

    /**
     * 返回的json数据
     *
     * @param obj      对象
     * @param excludes 排除的属性
     */
    public void responseJson(Object obj, String[] excludes) throws IOException {
        JsonConfig jc = new JsonConfig();
        // 排除不需要转换的字段
        jc.setExcludes(excludes);
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setHeader("Content-Type", "text/json;charset=utf-8");

        // 创建json对象
        if (obj instanceof Collection) {
            JSONArray json = JSONArray.fromObject(obj, jc);
            response.getWriter().write(json.toString());
        } else {
            JSONObject jsonObject = JSONObject.fromObject(obj, jc);
            response.getWriter().write(jsonObject.toString());
        }
    }

    /**
     * 分页参数
     **/
    protected int page;

    protected int limit;

    protected PageBean<T> pb = new PageBean<T>();

    public void setPage(int page) {
        this.page = page;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public void setIds(String ids) {
        this.ids = ids;
    }

    /**
     * 响应 返回字符串
     *
     * @param message 消息字符串
     * @throws IOException 异常
     */
    public void responseStr(String message) throws IOException {
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(message);
    }

    /*public Map<String, List<Object>> getUpdateHql(Map<String, Object> param, Map<String, Object> condition) {
        Map<String, List<Object>> map = new HashMap<String, List<Object>>();
        List<Object> list = new ArrayList<Object>();
        Set<String> paramSet = param.keySet();
        Set<String> conditionSet = condition.keySet();
        String modelName = t.getClass().getSimpleName();
        String hql = "update " + modelName + " set ";

        return map;
    }*/

    @Override
    public T getModel() {
        return t;
    }

    /**
     * 添加
     */
    public abstract void save() throws IOException;

    /**
     * 修改
     */
    public abstract void edit();

    /**
     * 删除
     */
    public abstract void delete() throws IOException;

    /**
     * 获取list
     */
    public abstract void showList() throws IOException;
}
